﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace WindowsFormsApplication1
{
    public sealed class XmlSerializable
    {
        private static XmlDocument Serialize<T>(T rootclass)
        {
            string xml = StringSerialize(rootclass);
            XmlDocument doc = new XmlDocument();
            doc.PreserveWhitespace = true;
            doc.LoadXml(xml);
            return doc; 
        }

        private static string StringSerialize<T>(T rootclass)
        {
            TextWriter w = WriterSerialize(rootclass);
            string xml = w.ToString();
            w.Close();
            return xml.Trim();
        }

        private static TextWriter WriterSerialize<T>(T rootclass)
        {
            TextWriter w = new StringWriter();
            XmlSerializer s = new XmlSerializer(typeof(T));
            s.Serialize(w, rootclass);
            w.Flush();
            return w;
        }

        private static T Deserialize<T>(string xml)
        {
            TextReader reader = new StringReader(xml);
            return Deserialize<T>(reader);
        }

        private static T Deserialize<T>(TextReader reader)
        {
            XmlSerializer s = new XmlSerializer(typeof(T));
            T o = (T)s.Deserialize(reader);
            reader.Close();
            return o;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="solution"></param>
        /// <param name="fileName"></param>
        public static void WriteFile<T>(T solution, string fileName)
        {
            try
            {
                string xml = Serialize(solution).OuterXml;
                using (StreamWriter writer = new StreamWriter(fileName, false))
                {
                    writer.Write(xml.Trim());
                    writer.Flush();
                    writer.Close();
                }
            }
            catch (Exception ex)
            {
                Console.Out.WriteLine(ex.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static T ReadFile<T>(string fileName)
        {
            T _solution = default(T);
            try
            {
                string xml = string.Empty;
                using (StreamReader reader = new StreamReader(fileName))
                {
                    xml = reader.ReadToEnd();
                    reader.Close();
                }
                _solution = Deserialize<T>(xml);
            }
            catch (Exception ex)
            {
                Console.Out.WriteLine(ex.Message);
            }

            return (T)_solution;
        }
    }
}
